package com.example.market;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class FragmentRemen extends Fragment {

    ListView lv;

    ArrayList<String> names = new ArrayList<>();

    ArrayList<String> loadNums = new ArrayList<>();

    ArrayList<String> logos = new ArrayList<>();

    private MyAdpter adpter;
    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);

        Button download1 =view. findViewById(R.id.download1);
        Button download2 = view.findViewById(R.id.download2);
        Button download3 =view. findViewById(R.id.download3);


        lv = view.findViewById(R.id.lv);
        adpter = new MyAdpter();

        lv.setAdapter(adpter);


        download1.setOnClickListener(new OnClick());
        download2.setOnClickListener(new OnClick());
        download3.setOnClickListener(new OnClick());

        getData();


    }

    void getData(){
        String url="http://124.93.196.45:10001/dev-api/bs-app-store/app/list";
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                .url(url)
                .addHeader("Authorization", Myapp.token)
                .get()
                .build();
        Call call=client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String string = null;
                        try {
                            string = response.body().string();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        JSONObject object= null;
                        try {
                            object = new JSONObject(string);
                            JSONArray data = object.getJSONArray("data");
                            for (int i = 0; i < data.length(); i++) {

                                JSONObject jsonObject=data.getJSONObject(i);

                                names.add(jsonObject.getString("apkName"));
                                loadNums.add(jsonObject.getString("apkDownNum"));
                                logos.add(jsonObject.getString("logoPic"));
                            }
                            adpter.notifyDataSetInvalidated();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Log.v("market",string);
                    }
                });
            }
        });
    }

    class MyAdpter extends BaseAdapter {

        @Override
        public int getCount() {
            return names.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {

            view = LayoutInflater.from(getContext()).inflate(R.layout.search_list, null);

            ImageView imgs = view.findViewById(R.id.imageView);
            TextView tvname = view.findViewById(R.id.name);
            TextView tvNum = view.findViewById(R.id.count);

            Glide.with(getContext()).load("http://124.93.196.45:10001/dev-api/"+logos.get(position)).into(imgs);



            tvname.setText(names.get(position));
            tvNum.setText(loadNums.get(position));
            Button download=view.findViewById(R.id.download);

            download.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getContext(), "正在下载。。。", Toast.LENGTH_SHORT).show();

                    Handler handler=new Handler();
                    Runnable runnable=new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getContext(), "下载成功", Toast.LENGTH_SHORT).show();
                        }
                    };handler.postDelayed(runnable,2000);
                }
            });



            return view;
        }


    }

    private class OnClick implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            Toast.makeText(getContext(), "正在下载。。。", Toast.LENGTH_SHORT).show();

            Handler handler=new Handler();
            Runnable runnable=new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getContext(), "下载成功", Toast.LENGTH_SHORT).show();
                }
            };handler.postDelayed(runnable,2000);
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.activity_market, container, false);
    }
}